﻿using StoreProject.Enum;
using StoreProject.Forms.UserControls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StoreProject.Forms.Base
{
    public partial class BaseConfigForm : Form
    {
        protected FormState _formState = FormState.INIT;
        protected object _currentObj = null;

        public object CurrEntity
        {
            get { return _currentObj; }
            set { _currentObj = value; }
        }

        public FormState FormUpdateState
        {
            get { return this._formState; }
            set
            {
                this._formState = value;
            }
        }
        public BaseConfigForm()
        {
            InitializeComponent();
            this.configTooBar1.ItemClicked += new ToolStripItemClickedEventHandler(configTooBar1_ItemClicked);
            //this.configTooBar1.ItemClicked +=
        }
        void configTooBar1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem is ConfigToolButton)
            {
                ConfigToolButton btn = (ConfigToolButton)e.ClickedItem;
                if (btn.Tag != null)
                {
                    ConfigButtonType type = (ConfigButtonType)btn.Tag;
                    IConfigCommand cmd = ConfigFromFactory.Instance.GetCommand(type);
                    cmd.ExecuteCommand(this);
                }
            }
        }

        public virtual void Add()
        {
            _formState = FormState.ADD;
        }

        public void UpdateData()
        {
            if (CanUpdate())
            {
                OnUpdate();
            }
            else
            {
                MessageBox.Show("请选择要修改的数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        public virtual void OnUpdate()
        {
        }

        public void Delete()
        {
            if (CanUpdate())
            {
                OnDelete();
            }
            else
            {
                MessageBox.Show("请选择要删除的数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        public virtual void OnDelete()
        {

        }

        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            if (IsValid())
            {
                OnSave();
            }
        }

        /// <summary>
        /// 数据有效后，保存
        /// </summary>
        public virtual void OnSave()
        {

        }

        /// <summary>
        /// 查询
        /// </summary>
        public virtual void Query()
        {
        }

        /// <summary>
        /// 退出
        /// </summary>
        public virtual void Exit()
        {
            this.Close();
        }

        /// <summary>
        /// 验证数据有效性
        /// </summary>
        public virtual bool IsValid()
        {
            return true;
        }

        private bool CanUpdate()
        {
            if (_currentObj == null)
                return false;
            return true;
        }

        /// <summary>
        /// 取消
        /// </summary>
        public virtual void Cancel()
        {

        }

        /// <summary>
        /// 打印
        /// </summary>
        public virtual void Print()
        {
            MessageBox.Show("【打印】功能没有实现，不能使用！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }

        /// <summary>
        /// 导出
        /// </summary>
        public virtual void Export()
        {

        }
    }
}
